package org.xtext.example.mydsl.validation;

import java.util.HashSet;
import java.util.Set;

import org.xtext.example.mydsl.myDsl.Abs;
import org.xtext.example.mydsl.myDsl.App;
import org.xtext.example.mydsl.myDsl.Let;
import org.xtext.example.mydsl.myDsl.MyDslPackage;
import org.xtext.example.mydsl.myDsl.Nrif;
import org.xtext.example.mydsl.myDsl.Tapp;
import org.xtext.example.mydsl.myDsl.Uq;
import org.xtext.example.mydsl.myDsl.util.MyDslSwitch;

public class CheckDuplicateBoundVariable extends MyDslSwitch<Void>{
	private MyDslJavaValidator validator;
	private Set<String> variable = new HashSet<String>();
	
	public CheckDuplicateBoundVariable(MyDslJavaValidator validator) {
		super();
		this.validator = validator;
	}
	
	
	@Override
	public Void caseAbs(Abs abstraction){
		if(!variable.add(abstraction.getParam())){
			validator.acceptError("Duplicated bound variable", abstraction, MyDslPackage.Literals.ABS__PARAM, 0, "");
		}
		
		doSwitch(abstraction.getBody());
		return null;		
	}
	
	@Override
	public Void caseApp(App app){
		doSwitch(app.getT1());
		doSwitch(app.getT2());
		return null;
	}
	
	
	@Override
	public Void caseUq(Uq uq){
		doSwitch(uq.getTerm());
		return null;
	}
	
	@Override
	public Void caseTapp(Tapp tapp){
		doSwitch(tapp.getTerm());
		return null;
	}
	
	@Override
	public Void caseNrif(Nrif nrif){
		doSwitch(nrif.getNome().getTerm());
		return null;	
	}
	
	@Override
	public Void caseLet(Let let){		
		doSwitch(let.getT2());
		doSwitch(let.getT1());
		return null;
		
	}

}

	